package cn.wolfcode.luowowo.mgrsite.job;

import cn.wolfcode.luowowo.article.StatisVO;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisVOService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * spring定时器
 * 用于将redis中的数据持久化道mysql中
 */
@Component
public class RedisDataPersistenceJob {

    @Reference
    private IStrategyStatisVOService strategyStatisVOService;

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Scheduled(cron = "0/10 * * * * ? ")
    public void testWork(){
        System.out.println("------------------------------------- vo的持久化 start -----------------------------------------------------");
        //查询出所有的vo
        List<StrategyStatisVO> vos =
            strategyStatisVOService.listVo(RedisKeys.STRATEGY_STATIS_VO.join("*"));

        for (StrategyStatisVO vo : vos) {

            StatisVO statisVO = new StatisVO();
            BeanUtils.copyProperties(vo,statisVO);
            //持久化的mysql中
            strategyDetailService.updateStatisVo(statisVO);
        }
        System.out.println("------------------------------------- vo的持久化 end -------------------------------------------------------");
    }
}
